<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_Cluster"></title>
    <!--    <script type="text/javascript" src="../js/include-web.js"></script>-->
    <!--    <script type="text/javascript" include="animatedclusterlayer,ol@4.6.5" src=".ol/include-ol.js"></script>-->
    <link rel="stylesheet" href="./css/bootstrap.min.css" type="text/css"/>
    <link rel="stylesheet" href="./css/ol.css" type="text/css"/>
    <link rel="stylesheet" href="./css/ktol3.css" type="text/css"/>
    <link rel="stylesheet" href="./css/mapPop.css" type="text/css"/>
    <link href="./css/pretty.css" rel="stylesheet"/>
    <script
            type="text/javascript"
            include="bootstrap,widgets.alert"
            src="./js/include-web.js"
    ></script>
    <script type="text/javascript" src="./js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="./js/ol-debug.js"></script>
    <!-- <script type="text/javascript" src="./js/proj4.js"></script> -->
    <script
            type="text/javascript"
            src="./js/iclient-ol-es6.js"
    ></script>
    <script type="text/javascript" src="./js/olms.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
<div id="map" style="width: 100%;height:100%"></div>
<script type="text/javascript">
    // var map, url = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/map-world/rest/maps/World";
    var map, url = "https://iserver.supermap.io/iserver/services/map-world/rest/maps/World";
    map = new ol.Map({
        target: 'map',
        controls: ol.control.defaults({attributionOptions: {collapsed: false}})
            .extend([new ol.supermap.control.Logo()]),
        view: new ol.View({
            center: [0, 0],
            zoom: 2,
            projection: 'EPSG:4326',
            multiWorld: true
        })
    });
    var layer = new ol.layer.Tile({
        source: new ol.source.TileSuperMapRest({
            url: url,
            wrapX: true
        }),
        projection: 'EPSG:4326'
    });
    map.addLayer(layer);
    map.addControl(new ol.supermap.control.ScaleLine());
    var clusterSource = new ol.source.Cluster({
        distance: 40,
        source: new ol.source.Vector(),
        wrapX: false
    });
    var clusterLayer = new ol.layer.AnimatedCluster({
        name: 'Cluster',
        source: clusterSource,
        animationDuration: 700,
        // style: getStyle
        style: setClusterStyle


    });
    map.addLayer(clusterLayer);

    function addFeatures(nb) {
        var features = [];
        var xmax = 130, xmin = 80, ymax = 50, ymin = 20;
        for (var i = 0; i < nb; ++i) {
            features[i] = new ol.Feature(new ol.geom.Point([Math.floor(Math.random() * (xmax - xmin + 1) + xmin), Math.floor(Math.random() * (ymax - ymin + 1) + ymin)]));
            features[i].set('id', i);
        }
        clusterSource.getSource().clear();
        clusterSource.getSource().addFeatures(features);
    }

    addFeatures(2000);


    function getStyle(feature) {
        var styleCache = {};
        var size = feature.get('features').length;
        var style = styleCache[size];
        if (!style) {
            var color = size > 25 ? "192,0,0" : size > 8 ? "255,128,0" : "0,128,0";
            var radius = Math.max(8, Math.min(size * 0.75, 20));
            var dash = 2 * Math.PI * radius / 6;
            dash = [0, dash, dash, dash, dash, dash, dash];
            style = styleCache[size] = [new ol.style.Style({
                image: new ol.style.Circle({
                    radius: radius,
                    stroke: new ol.style.Stroke({
                        color: "rgba(" + color + ",0.5)",
                        width: 15,
                        lineDash: dash,
                        lineCap: "butt"
                    }),
                    fill: new ol.style.Fill({
                        color: "rgba(" + color + ",1)"
                    })
                }),
                text: new ol.style.Text({
                    text: size.toString(),
                    fill: new ol.style.Fill({
                        color: '#fff'
                    })
                })
            })
            ];
        }
        return style;
    }


    function setClusterStyle(feature) {
        var features = feature.get('features');
        var size = features.length;
        var style = new ol.style.Style({
            image: new ol.style.Circle({
                radius: 18,
                stroke: new ol.style.Stroke({
                    color: '#fff'
                }),
                fill: new ol.style.Fill({
                    color: '#3399CC'
                })
            }),
            text: new ol.style.Text({
                font: '15px sans-serif',
                text: size.toString(),
                fill: new ol.style.Fill({
                    color: '#fff'
                })
            })
        });
        return style;
    }

</script>
</body>
</html>
